var rest = require('./rest');//相对路径
//var filter = require('./filter');
/**
 * @param {[]} data 
 */
function dataFilter(data) {
    //将获取得到的json对象，再拼接成新的json对象
    //var oneData=JSON.parse(data);
    //var newData=data.candidates;

    //修改后
    return data.map(function (item) {
        return {
            name: item.address,
            X: item.location.x,
            Y: item.location.y
        };
    });
}
//获取线要素paths的坐标
function pathsFilter(data){
    var obj=[];
    for (let i = 0; i < data.length; i++) {
		var objItem = {};
		objItem.name = data[i].value;
        objItem.geometry = data[i].geometry.paths;
		obj.push(objItem);
        }
    return obj;
}
//获取面状要素环的坐标
function ringsFilter(data){
    var obj=[];
    for (let i = 0; i < data.length; i++) {
		var objItem = {};
		objItem.name = data[i].value;
        objItem.geometry = data[i].geometry.rings;
		obj.push(objItem);
        }
    return obj;
}
//获取所有名字并去重
function nameFilter(data){

    var obj=[];
    for (let i = 0; i < data.length; i++) {
        var objItem = {};
        //将全部字符装换成大写
        obj[i]= data[i].attributes.Name.toUpperCase();
    }

    var res = [];
    var json = {};
    for(var i = 0; i < obj.length; i++){
     if(!json[obj[i]]){
      res.push(obj[i]);
      json[obj[i]] = 1;
     }
    }
    return res;
}
/**
 * 去重后再模糊匹配
 * @param {string} queryName 
 * @param {[]} data 
 */
function fuzMatch(queryName,data){
    /*var len = data.length;
    var arr = [];
    var reg = new RegExp(queryName);
    for(var i=0;i<len;i++){
        //如果字符串中不包含目标字符会返回-1
        if(data[i].match(reg)){
            arr.push(data[i]);
        }
    }
    return arr;*/

    //第二种写法
    return data.filter(function (item) {
        return item.indexOf(queryName) >= 0;
    });

    //第三种写法
    //return data.filter(item => item.indexOf(queryName) >= 0);
} 

//模糊匹配解决“数字问题”
function fuzMatchObj(queryName,datas){
    var arr = [];
    var len = datas.length;
    if(len>=1){
        for(var i=0;i<len;i++){
            var str=datas[i].name;
            //如果字符串中不包含目标字符会返回-1
            if(str.indexOf(queryName)>=0){
                arr.push(datas[i]);
            }
        }
    }
    return arr;

}

//对模糊匹配的结果去重
function resultsFilter(data){
    var res=[];
    var repeat=false;
    if(data.length>0){
        var res=[data[0]];
        for(var i=1;i<data.length;i++){
            var repeat=false;
            var str1=data[i].name;
            var str2=data[i].X;
            var str3=data[i].Y;
            for(var j=0;j<res.length;j++){
                if(str1===res[j].name&&str2===res[j].X&&str3===res[j].Y){
                    repeat=true;
                    break;
                }
            }
            if(!repeat){
                res.push(data[i]);
            }
        }
    }
    return res;
}
//构建缓冲区输入的geometry格式对象
function createBufGeo(repose){
    var geo=[];
    var item={
        "paths":repose.results[0].geometry.paths
    };
    geo.push(item);
    return geoJSON={
        "geometryType":repose.results[0].geometryType,
        "geometries":geo
    }
}
//构建Identity输入的geometry格式对象
function createIdenGeo(input){
    return geoJSON={
        "rings":input.geometries[0].rings
    }
}
//构建距离查询输入的geometry格式对象
function createGeometry(requery){
    var query=requery.split(",");//字符分割

    var inputGeos={};
    inputGeos.geo1={
        "geometryType":"esriGeometryPoint",
        "geometry": {
            "x": parseFloat(query[0]),
            "y": parseFloat(query[1]),
            "spatialReference": {
                "wkid": 4326
            }
        }
    }
    inputGeos.geo2={
        "geometryType":"esriGeometryPoint",
        "geometry": {
            "x": parseFloat(query[2]),
            "y": parseFloat(query[3]),
            "spatialReference": {
                "wkid": 4326
            }
        }
    }
    return inputGeos;
}
//新的地理定位函数
function identyLocator(data){
    var obj=[];
    for(var i=0;i<data.results.length;i++){
        var reName=data.results[i].attributes.IdentyName;
        //用到rest.js文件中的queryLocator函数
        var reLocator=rest.queryLocator("JZXH",reName);
        obj.push(dataFilter(reLocator));
    }
    return obj;
}
module.exports = { dataFilter,pathsFilter,ringsFilter,nameFilter,fuzMatch,resultsFilter,fuzMatchObj,createGeometry,createBufGeo,createIdenGeo,identyLocator};//暴露函数